package cn.edu.sdjzu.xg2121.controller;

import cn.edu.sdjzu.xg2121.domain.School;
import cn.edu.sdjzu.xg2121.domain.Teacher;
import cn.edu.sdjzu.xg2121.exception.BysjException;
import cn.edu.sdjzu.xg2121.service.SchoolService;
import cn.edu.sdjzu.xg2121.service.TeacherService;
import cn.edu.sdjzu.xg2121.util.Condition;
import cn.edu.sdjzu.xg2121.util.ControllerHelper;
import cn.edu.sdjzu.xg2121.util.JSONUtil;
import cn.edu.sdjzu.xg2121.util.Pagination;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

@WebServlet("/schoolReadCtl.ctl")
public class SchoolReadCtl extends HttpServlet {
    /**
     * GET, http://localhost:8080/teacherReadCtl.ctl?id=1，查询 id=1的学院把一个学院对象响应到前端
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        //设置响应字符编码为UTF-8，以便正确响应汉字
        response.setContentType("application/json;charset=UTF-8");
        //创建JSON对象message，以便往前端响应信息
        JSONObject message = new JSONObject();
        //获得id
        String id_str = request.getParameter("id");
        Integer id_int = Integer.parseInt(id_str);
        try{
            School school = SchoolService.getInstance().find(id_int);
            String school_json = JSON.toJSONString(school);
            //响应 message到前端
            response.getWriter().println(school_json);
        }catch (BysjException e){
            message.put("message", e.getMessage());
            response.getWriter().println(message);
            e.printStackTrace();
        }catch (SQLException e){
            message.put("message", "数据库操作异常");
            response.getWriter().println(message);
            e.printStackTrace();
        }catch(Exception e){
            message.put("message", "网络异常");
            response.getWriter().println(message);
            e.printStackTrace();
        }
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置响应字符编码为UTF-8
        response.setContentType("application/json;charset=UTF-8");//创建JSON对象message，以便往前端响应信息
        JSONObject message_jsonObj = new JSONObject();
        String req_jsonStr = JSONUtil.getJSON(request);
        JSONObject req_jsonObj = JSON.parseObject(req_jsonStr,JSONObject.class);
        if (req_jsonObj == null) {
            message_jsonObj.put("message", "未收到请求信息");
            response.getWriter().println(message_jsonObj);
            return;
        }
        try {
            Pagination pagination = ControllerHelper.getPagination(req_jsonObj);
            List<Condition> conditionList = ControllerHelper.getConditions(req_jsonObj);
            Collection<School> schools = SchoolService.getInstance().findAll(conditionList,pagination);
            message_jsonObj.put("totalNum",pagination.getTotalNum());
            message_jsonObj.put("records",schools);
            response.getWriter().println(message_jsonObj);
        } catch (BysjException e) {
            message_jsonObj.put("message", e.getMessage());
            response.getWriter().println(message_jsonObj);
            e.printStackTrace();
        }catch (SQLException e) {
            message_jsonObj.put("message", "数据库操作异常");
            response.getWriter().println(message_jsonObj);
            e.printStackTrace();
        } catch (Exception e) {
            message_jsonObj.put("message", "网络异常");
            response.getWriter().println(message_jsonObj);
            e.printStackTrace();
        }
    }
}
